package com.qifan.alb2b.action.front;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qifan.alb2b.action.front.param.PersonNum;
import com.qifan.alb2b.domain.OrderInfoDomain;
import com.qifan.alb2b.exception.BusinessException;
import com.qifan.alb2b.service.BookService;
import com.qifan.alb2b.service.vo.RouteVo;
import com.qifan.alb2b.service.vo.UserVo;
import com.qifan.alb2b.util.Constants;
import com.qifan.alb2b.util.DateUtil;
import com.qifan.alb2b.util.ID;

/**
 * 预定，生成订单
 * @author hc24
 *
 */
@RequestMapping("/book")
@Controller
public class BookControler {
	private BookService bookService;
	
	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}

	@RequestMapping("/toBook.usr")
	public String toBook(HttpServletRequest request){
		HttpSession session=request.getSession(true);
		RouteVo routeVo=(RouteVo) session.getAttribute(Constants.SESSION_ROUTE_VO);
		if(routeVo==null)
			throw new BusinessException("请重新选择线路");
		
		request.setAttribute("route", routeVo.getRoute());
		request.setAttribute("spt", routeVo.getStPriceTable());
		return "pages/front/create_order";
	}
	
	@RequestMapping("/getPriceTable.usr")
	public String getPriceTable(HttpServletRequest request, PersonNum personNum){
		try{
			HttpSession session=request.getSession(true);
			RouteVo routeVo=(RouteVo) session.getAttribute(Constants.SESSION_ROUTE_VO);
			if(routeVo==null)
				throw new BusinessException("请重新选择线路");
			double[] totalPrice=bookService.calcTotalPrice(routeVo, personNum);
			double totalSumPrice=0;
			for(int i=0;i<totalPrice.length;i++)
				totalSumPrice += totalPrice[i];
			
			request.setAttribute("spt", routeVo.getStPriceTable());
			request.setAttribute("totalPrices", totalPrice);
			request.setAttribute("totalSumPrice", totalSumPrice);
			request.setAttribute("index", personNum.getIndex());
			return "pages/front/book/price_table";
		}catch(Exception e){
			//个性化的处理异常
			request.setAttribute("exception", e.getMessage());
			request.setAttribute("stack", e.toString());
			return "pages/common/error2";
		}
	}
	
	@RequestMapping("/book.usr")
	public String createOrder(HttpServletRequest request,OrderInfoDomain orderInfo){
		HttpSession session=request.getSession(true);
		RouteVo routeVo=(RouteVo) session.getAttribute(Constants.SESSION_ROUTE_VO);
		if(routeVo==null)
			throw new BusinessException("请重新选择线路");
		UserVo userVo=(UserVo) session.getAttribute(Constants.SESSION_USER_VO);
		
		//验证人数
		if(routeVo.getGroupType()==Constants.GROUP_TYPE_SIX&&(orderInfo.getTotalCount()==0||orderInfo.getTotalCount()>6))
			throw new BusinessException("超出1-6人团的最大人数限制");
		if(routeVo.getGroupType()==Constants.GROUP_TYPE_SEVEN&&(orderInfo.getTotalCount()<7||orderInfo.getTotalCount()>38))
			throw new BusinessException("超出7-38人团的最大人数限制或少于7人");
		
		bookService.book(userVo,routeVo, orderInfo);
		request.setAttribute("orderId", orderInfo.getOrderId());
		request.setAttribute("payTime", DateUtil.format(orderInfo.getPayTime(), "yyyy年MM月dd日 HH:mm:ss"));
		//去除session中的routeVo
		session.removeAttribute(Constants.SESSION_ROUTE_VO);
		session.removeAttribute("type");
		return "pages/front/order_success";
	}
	
	@RequestMapping("/printOrder.usr")
	public String printOrder(HttpServletRequest request,OrderInfoDomain orderInfo){
		request.setAttribute("orderInfo", orderInfo);
		return "pages/front/book_print";
	}
}
